package com.edgardeng.producer;

import com.edgardeng.entity.Merchant;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:rabbitmq.properties")
public class RabbitSender {

    @Value("${route.key.direct}")
    private String directRouteKey;

    @Value("${com.edgardeng.exchange.direct}")
    private String directExchange;
    @Value("${com.edgardeng.exchange.topic}")
    private String topicExchange;
    @Value("${com.edgardeng.exchange.fanout}")
    private String fanoutExchange;


    private String topicRouteKey1 = "key.topic.abc";
    private String topicRouteKey2 = "key.topic.abc.adn";


    @Autowired
    AmqpTemplate rabbitTemplate;

    public void send() throws JsonProcessingException {
        Merchant m = new Merchant("100","edgar", "Shanghai");
        rabbitTemplate.convertAndSend(directExchange,directRouteKey, m);
//        rabbitTemplate.send();
        rabbitTemplate.convertAndSend(topicExchange, topicRouteKey1, "topic msg: shanghai ");
        rabbitTemplate.convertAndSend(topicExchange, topicRouteKey2, "topic msg: beijing ");

        // 发送 json 字符串
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(m);
        System.out.println(json);
        rabbitTemplate.convertAndSend(fanoutExchange, "", json);

    }


}
